package com.example.a1.util;

import android.os.Handler;
import android.os.Looper;
import com.example.a1.model.Banner;
import com.example.a1.model.Category;
import com.example.a1.model.Course;
import java.util.ArrayList;
import java.util.List;

public class HttpUtil {

    private static final Handler mainHandler = new Handler(Looper.getMainLooper());

    public interface OnDataLoadedListener<T> {
        void onSuccess(T data);
        void onFailure(String error);
    }

    // 模拟获取轮播图数据，每个轮播图关联专属详情 URL（非课程 URL）
    public static void getBannerData(final OnDataLoadedListener<List<Banner>> listener) {
        new Thread(() -> {
            try {
                Thread.sleep(800);
                List<Banner> banners = new ArrayList<>();
                // 轮播图 1：Android 开发实战（专属详情 URL）
                banners.add(new Banner(
                        1,
                        "https://img95.699pic.com/xsj/0t/4v/aw.jpg!/fh/300",
                        "Android开发实战",
                        "https://api.example.com/banner/detail/1"
                ));
                // 轮播图 2：Java 编程基础（专属详情 URL）
                banners.add(new Banner(
                        2,
                        "https://img95.699pic.com/xsj/0u/41/9q.jpg!/fh/300",
                        "Java编程基础",
                        "https://api.example.com/banner/detail/2"
                ));
                // 轮播图 3：Python 数据分析（专属详情 URL）
                banners.add(new Banner(
                        3,
                        "https://img95.699pic.com/xsj/0k/4f/28.jpg!/fh/300",
                        "Python数据分析",
                        "https://api.example.com/banner/detail/3"
                ));
                mainHandler.post(() -> listener.onSuccess(banners));
            } catch (Exception e) {
                mainHandler.post(() -> listener.onFailure(e.getMessage()));
            }
        }).start();
    }

    // 模拟获取分类数据
    public static void getCategoryData(final OnDataLoadedListener<List<Category>> listener) {
        new Thread(() -> {
            try {
                Thread.sleep(800);
                List<Category> categories = new ArrayList<>();
                categories.add(new Category(1, "编程语言", "", "https://api.example.com/category/1"));
                categories.add(new Category(2, "移动开发", "", "https://api.example.com/category/2"));
                categories.add(new Category(3, "前端开发", "", "https://api.example.com/category/3"));
                categories.add(new Category(4, "后端开发", "", "https://api.example.com/category/4"));
                mainHandler.post(() -> listener.onSuccess(categories));
            } catch (Exception e) {
                mainHandler.post(() -> listener.onFailure(e.getMessage()));
            }
        }).start();
    }

    // 模拟获取推荐课程数据
    public static void getCourseData(final OnDataLoadedListener<List<Course>> listener) {
        new Thread(() -> {
            try {
                Thread.sleep(800);
                List<Course> courses = new ArrayList<>();
                courses.add(new Course(1, "Android高级开发", "张教授", "中级", 4.8f,
                        "https://img95.699pic.com/xsj/0f/6o/v2.jpg!/fh/200", "https://api.example.com/course/1"));
                courses.add(new Course(2, "Java并发编程", "李老师", "高级", 4.7f,
                        "https://img95.699pic.com/xsj/03/1b/v4.jpg!/fh/200", "https://api.example.com/course/2"));
                courses.add(new Course(3, "React Native入门", "王讲师", "初级", 4.5f,
                        "https://img95.699pic.com/xsj/02/55/8m.jpg!/fh/200", "https://api.example.com/course/3"));
                courses.add(new Course(4, "Python机器学习", "赵博士", "高级", 4.9f,
                        "https://img95.699pic.com/xsj/07/8k/2u.jpg!/fh/200", "https://api.example.com/course/4"));
                mainHandler.post(() -> listener.onSuccess(courses));
            } catch (Exception e) {
                mainHandler.post(() -> listener.onFailure(e.getMessage()));
            }
        }).start();
    }

    // 模拟根据分类 ID 获取对应课程数据
    public static void getCoursesByCategoryId(int categoryId, final OnDataLoadedListener<List<Course>> listener) {
        new Thread(() -> {
            try {
                Thread.sleep(800);
                List<Course> courses = new ArrayList<>();
                switch (categoryId) {
                    case 1:
                        courses.add(new Course(101, "Java基础入门", "李老师", "初级", 4.6f,
                                "https://img95.699pic.com/xsj/03/1b/v4.jpg!/fh/200", "https://api.example.com/course/101"));
                        courses.add(new Course(102, "Python数据分析", "赵博士", "中级", 4.8f,
                                "https://img95.699pic.com/xsj/07/8k/2u.jpg!/fh/200", "https://api.example.com/course/102"));
                        break;
                    case 2:
                        courses.add(new Course(201, "Android高级开发", "张教授", "中级", 4.8f,
                                "https://img95.699pic.com/xsj/0f/6o/v2.jpg!/fh/200", "https://api.example.com/course/201"));
                        courses.add(new Course(202, "iOS开发实战", "王讲师", "高级", 4.7f,
                                "https://img95.699pic.com/xsj/02/55/8m.jpg!/fh/200", "https://api.example.com/course/202"));
                        break;
                    case 3:
                        courses.add(new Course(301, "React入门到精通", "刘老师", "中级", 4.9f,
                                "https://img95.699pic.com/xsj/0a/2d/40.jpg!/fh/200", "https://api.example.com/course/301"));
                        courses.add(new Course(302, "Vue实战项目", "陈讲师", "初级", 4.5f,
                                "https://img95.699pic.com/xsj/06/79/6o.jpg!/fh/200", "https://api.example.com/course/302"));
                        break;
                    case 4:
                        courses.add(new Course(401, "SpringBoot实战", "黄教授", "高级", 4.8f,
                                "https://img95.699pic.com/xsj/0m/7k/5i.jpg!/fh/200", "https://api.example.com/course/401"));
                }
                mainHandler.post(() -> listener.onSuccess(courses));
            } catch (Exception e) {
                mainHandler.post(() -> listener.onFailure(e.getMessage()));
            }
        }).start();
    }

    // 模拟获取轮播图专属详情，根据 URL 返回对应内容
    public static void getBannerDetail(String url, final OnDataLoadedListener<String> listener) {
        new Thread(() -> {
            try {
                Thread.sleep(800);
                final String[] detail = {""}; // 用数组存储，解决 Lambda 变量约束问题
                if (url.contains("banner/detail/1")) {
                    detail[0] = "Android开发实战专属详情：本课程涵盖组件化、热修复、性能优化等高级技术，包含20个企业级实战案例，学习后可独立开发复杂Android应用。";
                } else if (url.contains("banner/detail/2")) {
                    detail[0] = "Java编程基础专属详情：从变量、语法到面向对象编程，循序渐进讲解Java核心知识，配套500+练习题，适合零基础入门。";
                } else if (url.contains("banner/detail/3")) {
                    detail[0] = "Python数据分析专属详情：基于Pandas、Matplotlib等库，讲解数据清洗、可视化、机器学习建模，附真实数据集实战。";
                }
                mainHandler.post(() -> listener.onSuccess(detail[0]));
            } catch (Exception e) {
                mainHandler.post(() -> listener.onFailure(e.getMessage()));
            }
        }).start();
    }

    // 模拟获取课程详情，严格根据 URL 匹配返回对应课程信息
    public static void getCourseDetail(String detailUrl, final OnDataLoadedListener<Course> listener) {
        new Thread(() -> {
            try {
                Thread.sleep(800);
                Course course = null;
                if (detailUrl.equals("https://api.example.com/course/1")) {
                    course = new Course(1, "Android高级开发", "张教授", "中级", 4.8f,
                            "https://img95.699pic.com/xsj/0f/6o/v2.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/2")) {
                    course = new Course(2, "Java并发编程", "李老师", "高级", 4.7f,
                            "https://img95.699pic.com/xsj/03/1b/v4.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/3")) {
                    course = new Course(3, "React Native入门", "王讲师", "初级", 4.5f,
                            "https://img95.699pic.com/xsj/02/55/8m.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/4")) {
                    course = new Course(4, "Python机器学习", "赵博士", "高级", 4.9f,
                            "https://img95.699pic.com/xsj/07/8k/2u.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/101")) {
                    course = new Course(101, "Java基础入门", "李老师", "初级", 4.6f,
                            "https://img95.699pic.com/xsj/03/1b/v4.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/102")) {
                    course = new Course(102, "Python数据分析", "赵博士", "中级", 4.8f,
                            "https://img95.699pic.com/xsj/07/8k/2u.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/201")) {
                    course = new Course(201, "Android高级开发", "张教授", "中级", 4.8f,
                            "https://img95.699pic.com/xsj/0f/6o/v2.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/202")) {
                    course = new Course(202, "iOS开发实战", "王讲师", "高级", 4.7f,
                            "https://img95.699pic.com/xsj/02/55/8m.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/301")) {
                    course = new Course(301, "React入门到精通", "刘老师", "中级", 4.9f,
                            "https://img95.699pic.com/xsj/0a/2d/40.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/302")) {
                    course = new Course(302, "Vue实战项目", "陈讲师", "初级", 4.5f,
                            "https://img95.699pic.com/xsj/06/79/6o.jpg!/fh/200", detailUrl);
                } else if (detailUrl.equals("https://api.example.com/course/401")) {
                    course = new Course(401, "SpringBoot实战", "黄教授", "高级", 4.8f,
                            "https://img95.699pic.com/xsj/0m/7k/5i.jpg!/fh/200", detailUrl);
                }
                final Course tempCourse = course;
                mainHandler.post(() -> {
                    if (tempCourse != null) {
                        listener.onSuccess(tempCourse);
                    } else {
                        listener.onFailure("未找到对应课程详情");
                    }
                });
            } catch (Exception e) {
                mainHandler.post(() -> listener.onFailure(e.getMessage()));
            }
        }).start();
    }
}